﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Threading;
using KOF.ViewModels.Commands;
using KOF.Helpers;
using Newtonsoft.Json;
using System.IO.IsolatedStorage;
using System.Collections.Generic;

namespace KOF.ViewModels
{
    public class FavoriteViewModel : ViewModelBase
    {
        public FavoriteViewModel()
        {
            getFavoriteList();

            // calling analytics
            GoogleAnalytics.EasyTracker.GetTracker().SendView("Favorite tab");
        }

        private System.Collections.ObjectModel.ObservableCollection<ReleaseResult> _listItemSource;
        public System.Collections.ObjectModel.ObservableCollection<ReleaseResult> ListItemSource
        {
            get
            {
                return _listItemSource;
            }
            set
            {
                if (_listItemSource != value)
                {
                    _listItemSource = value;
                }
                OnPropertyChanged("ListItemSource");
            }
        }

        public void getFavoriteList()
        {
            if (IsolatedStorageSettings.ApplicationSettings.Contains("FavoritesList"))
            {
                List<KOF.Helpers.ReleaseResult> lstFav = (List<KOF.Helpers.ReleaseResult>)IsolatedStorageSettings.ApplicationSettings["FavoritesList"];

                if (lstFav != null)
                {
                    if (ListItemSource == null) ListItemSource = new System.Collections.ObjectModel.ObservableCollection<ReleaseResult>();
                    foreach (ReleaseResult item in lstFav)
                    {
                        ListItemSource.Add(item);
                        ReleaseResult temp = new ReleaseResult();
                        temp.id = item.id;
                        temp.title = item.title;
                        temp.description = item.description;
                        temp.price = item.price;
                        temp.style = item.style;
                        temp.iphone_images = item.iphone_images;
                        temp.event_date = item.event_date;
                        temp.master_image = item.master_image;
                        temp.type = item.type;
                        temp.thumb_iphone_master_image = item.thumb_iphone_master_image;
                        temp.IsDownloading = true;
                        temp.kof_link = item.kof_link;
                        (App.Current as App).lstReleaseResult.Remove(item);
                        (App.Current as App).lstReleaseResult.Add(temp);
                        if (item.title.Length > 28)
                        {
                            item.title = item.title.Substring(0, 24) + "...";
                        }
                        if (item.style.Length > 28)
                        {
                            item.style = item.style.Substring(0, 24) + "...";
                        }

                    }
                }
            }
        }


    }
}
